Okay, so in today's lesson we are going to start off quite a large area of the course called functions.
Now we've actually already looked at some functions.
We've already looked at a print function, the wait function the warn function instance dot new function.
But this this part we're going to learn a bit more about them, including some of the different types,
as well as how to create your own link from the things called events.
Return from them.
There's a lot to go over.
Okay?
Now, functions are something which are quite a lot of people find quite difficult.
And there are they are a step up from the coding we've already done.
However, once you have finished all the lessons on functions, you will suddenly be able to decode
a lot more things than you would do already.
Okay, so even though it might be difficult and might take you a bit longer to learn than some of the
other stuff, it's absolutely worth it.
And I would definitely not skip these lectures.
Okay, um, so add a script into your game.
We're just looking at normal server scripts for now.
We're going to go off local scripts for a bit, and we'll come back to them a bit later on.
And we're going to have a look at the first type of function.
Okay.
So some functions in Roblox Studio have already been created for you.
Um, these functions you can use like like like the print function.
Okay.
You can use them um, just by putting in, saying the name of a function and putting in any extra information
in the pair of brackets.
So the print function just like this.
Okay.
Um, these functions, the reason why they exist is because actually code, um, some text going from
your script here into the output would actually be a process that would take hundreds of lines, okay.
Which would be would be really difficult for us to even, even me even to understand it'd be very advanced
coding.
Um, so instead, to make it easier, um, Roblox instead creates a print function for us, which we
can use.
Okay.
And we're going to have a look at some of those, but we're going to have a look at some that we kind
of attach to the parts instead.
So um, there are lots of different types of these functions.
Now this is more of an introduction to them rather than a complete overview, because you'll be introduced
to to a lot more as you go along in the course.
So this is more of an introduction and but you know, maybe a bit more about this as you go on later
in the course.
Okay.
So let's first off add a part into our game.
Local part is equal to game dot workspace dot part okay.
So the first one we're going to have a look at is called clone.
And this allows you to clone the part.
So to create a whole new copy of the part okay.
So um to do this you first reference the object that you're wanting this function to work on, which
in this case is the part.
Okay.
Um, then you want to attach this function onto it.
Now the function isn't a part, so I shouldn't say shouldn't say it.
It's not.
It's not a, um it's not inside of the part.
It's something you attach onto it, as I said.
So you don't use dot because it's not a member of instead you use colon okay.
And you can see here here are a list of all the different functions you can add onto it.
So a lot okay.
Which is why we're only going over a couple today.
Otherwise this lesson would literally be hours long.
Um so part colon clone you can see here.
And we press enter.
It looks something like this.
Okay.
And that built in function would allow you to clone your object.
So this clones your object.
And of course it wouldn't just work for parts, it would work for anything.
However, for new topics like this, I usually just use parts to learn to start off with because they're
easiest and most visual to understand.
Okay, um, if you've cloned it, you might also want to know how to destroy it.
Okay, so to delete or destroy an object you would write the object colon and then we destroy keyword
here okay.
And that would allow you to delete the part or delete your object okay.
So they are quite simple really.
Um it's just a colon.
And then the name of what it is repair brackets okay.
And again these allow you to do.
Quite complex things very easily because they've already been set up for you to use.
Okay.
The next two involve you putting a bit more information inside of these brackets.
Okay, so the first one is part colon.
Fine.
First child.
Okay.
Now what?
Fine.
First child does is that it tries to find a child of your object with a given name.
And you can give it the name, but it's going to try to find.
Okay.
Um, now, the advantage of using this instead of just saying so say I wanted to find a part called
fire.
Okay?
And I would write in here in speech marks fire like that there.
Okay.
The advantage of doing that instead of just saying game dot workspace instead of saying part dot fire.
Like that here is because we already know that you can't really reference something that doesn't exist
to start off with.
Okay, even if you're adding it on later on, it won't really let you, okay.
Which is why you can't really use that method.
Okay.
Um, so the find first child will attempt to find it.
Okay.
Um, but the good thing with this is that if this fire object does not exist, that's completely fine.
Okay, it won't throw an error.
And that's the main key difference with this.
Okay.
If you did part fire and fire does not exist.
It will throw an error.
But this here would not.
So it's all about just checking if something does exist rather than saying it does exist, and then
doing code from it.
Okay, so this attempts to find a child with a given name okay.
And basically what would happen is if it finds the child then this would be the value of the child.
If it doesn't find the child this would have the value of nil.
And if you haven't heard of nil before, nil basically means nothing.
Okay not zero because zero is a number.
Nil just means nothing.
Okay, so if you really wanted to check to see if it existed, you'd have to put this in an if statement
and say something like if part colon find first child fire, then print fire exists.
Oh.
Else print.
Fire does not exist.
Okay, so that's really how you'd use that in an actual bit of script.
Okay.
On to the next one, which is very similar.
And some sometimes people get them confused.
Um, is part colon wait for child.
Okay.
Now wait for child.
Um, we'll basically wait for a child to appear.
So, for example, last lesson, we looked at the player and the character, and the character takes
slightly longer to load than the player for certain reasons.
Okay, same with a lot of other objects.
A lot of other objects take a lot longer to load than others.
Um, so sometimes when you try to reference them normally, it will throw an error because they don't
exist yet.
You're trying to find something that doesn't exist yet.
Um, so to reference those things you can use wait for child.
Okay.
And this will basically wait for a child of a given name to exist.
So if I wanted to do, um, my character, for example, I could say game dot workspace colon, wait
for sorry, wait for child and then my username Cardillo.
Okay.
And it'll wait for my character to appear.
Okay.
Um, now, a good idea with this is that currently it will basically wait forever.
Okay, now, if your object doesn't appear for some reason, your script will basically be just waiting
here forever and it won't do anything else.
So it's a good idea to put a timeout for around ten maybe 20s.
Okay.
And yeah, so it's in seconds, by the way.
And um, if after those 20s it cannot find the child, it doesn't, it doesn't see the child yet, then
it then it will throw an error.
Okay.
Now I know we don't want to.
I know we don't like to deal with errors, but it at least stops the script from, um, being being
stuck state where you can't do anything or it just it's just broken.
You can't do anything with it.
Okay.
So it's a good idea to sometimes put a timeout here as well.
Okay.
So let me write a little description of it here for you.
Um, waits for a child to appear.
Okay, so there are four major functions that you can attach onto your objects.
Okay.
As I showed you, there are so many more.
Probably hundreds.
Okay.
Um, you'll be introduced gradually to these as we go on to different topics.
Okay.
So this is not a entire list of all of them at all, but the main reason of this is to give you some
common ones that you might want to use.
Um, and so you know how to write them if you needed to.
Again.
Okay.
Um, now, just before I finish, I just want to clarify something.
Um, I did kind of lie to you a bit in the first one.
Okay?
And you might guess the reason why.
Um, just like when you add an object into the game instance.
Dot new looks about a few lessons ago.
You need to give it a parent.
Just like how if you clone a part, you also need to give it a parent.
So really, how you supposed to do that would be to say local new part is equal to part colon clone
and then say new part dot parent is equal to game dot workspace.
Or if it's a different object wherever else it should go okay.
So make sure that you're giving it a parent in order for it to work.
Okay.
But destroy find first child and wait for child.
They can all work on one line as well, so hopefully that gives you a good overview of some of these
functions.
And again you'll learn a lot more about them later on.
And join me in the next lesson where we're going to actually have a look at how to create our own functions.